﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BrainTechLLC;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FullImageView.xaml
    /// </summary>
    public partial class DirectoriesView : System.Windows.Controls.UserControl
    {
        public static readonly DependencyProperty DirectoriesToExamineViewModelProperty =
            DependencyProperty.Register("DirectoriesToExamineViewModel",typeof(DirectoriesToExamineViewModel),typeof(DirectoriesView),new PropertyMetadata(null, OnDirectoriesToExamineViewModelPropertyChanged));

        public DirectoriesToExamineViewModel DirectoriesToExamineViewModel
        {
            get { return (DirectoriesToExamineViewModel)GetValue(DirectoriesToExamineViewModelProperty); }
            set { SetValue(DirectoriesToExamineViewModelProperty, value); }
        }

        private static void OnDirectoriesToExamineViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DirectoriesView source = d as DirectoriesView;
            DirectoriesToExamineViewModel v = e.NewValue as DirectoriesToExamineViewModel;
            source.UpdateDirectoriesToExamineViewModel(v);
        }

        private void UpdateDirectoriesToExamineViewModel(DirectoriesToExamineViewModel viewModel)
        {
            DirectoriesToExamineViewModel = viewModel;
            this.DataContext = DirectoriesToExamineViewModel;
        }

        public DirectoriesView()
        {
            InitializeComponent();
        }

        public List<DirectoryToExamineViewModel> GetSelected()
        {
            return lstItems.GetSelected<DirectoryToExamineViewModel>();            
        }

        private void lstItems_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                var dirs = GetSelected();

                for (int n = 0; n < dirs.Count; n++)
                {
                    DirectoriesToExamineViewModel.RemoveDir(dirs[n].Directory);
                }
            }
        }

        private void btnAddDir_Click(object sender, RoutedEventArgs e)
        {
            var dir = Directory.GetCurrentDirectory();

            FolderBrowserDialog dialog = new FolderBrowserDialog()
            {
                SelectedPath = dir,
                ShowNewFolderButton = true,
                RootFolder = Environment.SpecialFolder.MyComputer,
                Description = "Test"
            };

            var result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                DirectoriesToExamineViewModel.AddDir(dialog.SelectedPath);
            }
        }
    }
}
